package com.tanhua.server.controller;

import com.tanhua.server.service.TanHuaService;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author feihao
 * @version 1.0
 * @date 2021/7/8 20:13
 */

@RestController
@RequestMapping("tanhua")
public class TanHuaController {

    @Autowired
    private TanHuaService tanHuaService;


    /**
     * 查询个人主页信息接口：
     *
     * @param userId
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity<TodayBest> queryPersonInfo(@PathVariable("id") Long userId) {

        try {
            TodayBest personInfo = tanHuaService.queryPersonInfo(userId);
            return ResponseEntity.ok(personInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜附近接口：
     *
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> searchNearUsers(@RequestParam(value = "gender", required = false) String gender,
                                                            @RequestParam(value = "distance", required = false, defaultValue = "2000") Double distance) {

        try {
            List<NearUserVo> nearUsersList = tanHuaService.searchNearUsers(gender, distance);
            return ResponseEntity.ok(nearUsersList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询陌生人问题：
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> findQuestion(@RequestParam("userId") Long userId) {

        try {
            String question = tanHuaService.findQuestion(userId);
            return ResponseEntity.ok(question);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 回复问题接口：
     *
     * @param mapParam
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replyQuestion(@RequestBody Map<String, Object> mapParam) {

        Long userId = Long.valueOf(mapParam.get("userId").toString());
        String reply = mapParam.get("reply").toString();
        try {
            Boolean b = tanHuaService.replyQuestion(userId, reply);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询探花cardList推荐：
     *
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardList() {

        try {
            List<TodayBest> todayBestList = tanHuaService.queryCardList();
            return ResponseEntity.ok(todayBestList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 左滑喜欢这个用户：
     *
     * @param loveUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> loveUser(@PathVariable("id") Long loveUserId) {

        try {
            Boolean b = tanHuaService.loveUser(loveUserId);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 右滑 不喜欢
     *
     * @param disLoveUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> disLoveUser(@PathVariable("id") Long disLoveUserId) {

        try {
            Boolean b = tanHuaService.disLoveUser(disLoveUserId);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
}
